<?php
namespace app\manage\controller;

class AuditCpsApply extends Base
{
    //CPS对账申请
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $id = input('id');
            $channelId = input('channel_id');

            $where = [];
            !empty($id) && $where['id'] = $id;
            !empty($channelId) && $where['cps_id'] = $channelId;

            if( ! $count=model('CpsBillApply')->where($where)->count() ){
               $this->layTable();
            }

            $select = model('CpsBillApply')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('apply_time desc')
                ->select();

            $data = [];
            $channelIdName = model('Channel')->channelIdName();
            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'cps_id' => $v['cps_id'],
                    'cps_name' => $channelIdName[ $v['cps_id'] ],
                    'apply_time' => date('Y-m-d H:i:s', $v['apply_time']),
                    'status' => $v['status'] ? '已处理' : '未处理',
                ];
            }

            $this->layTable(0, 'CPS对账申请列表', $data, $count);
        }else{
            $channelData = model('ViewChannel')->field('cps_id,cps_name')->where('channel_id', 1000)->order('cps_id desc')->select();
            return view('', ['channel_data'=>$channelData]);
        }
    }

    //批量确认处理
    public function oneSure(){
        $allId = input('all_id');
        if(empty($allId)){
            $this->layJson(1, '请勾选复选框');
        }
        if( model('CpsBillApply')->save(['status'=>1], ['id'=>['in', $allId]]) ){
            $this->layJson(0, '确认成功');
        }else{
            $this->layJson(1, '确认失败，请重试');
        }
    }

    //限制申请时间
    public function limitTime(){
        if(request()->isAjax()){
            if(!empty(input('date'))){
                $date = explodeTime(input('date'));
                $save['name'] = $date['start_time'];
                $save['value'] = $date['end_time'];
            }else{
                $save['name'] = '';
                $save['value'] = '';
            }
            if( model('CommonOption')->save($save, ['id'=>222]) ){
                $this->layJson(0, '修改成功');
            }else{
                $this->layJson(1, '修改失败，请重试');
            }
        }else{
            $find = model('CommonOption')->field('name, value')->where('id', 222)->find();
            if($find['name'] && $find['value']){
                $date = date('Y-m-d', $find['name']) . ' - ' . date('Y-m-d', $find['value']);
            }else{
                $date = '';
            }

            return view('', ['date'=>$date]);
        }
    }

    //设置公告
    public function setNotice(){
        if(request()->isAjax()){
            $save1['big_des'] = input('audit_apply_notice');
            $rs1 = model('CommonOption')->save($save1, ['id'=>250]);

            $save2['big_des'] = input('audit_apply_content');
            $rs2 = model('CommonOption')->save($save2, ['id'=>260]);

            if($rs1!==false && $rs2!==false){
                $this->layJson(0, '修改成功');
            }else{
                $this->layJson(1, '修改失败，请重试');
            }
        }else{
            $auditApplyNotice = model('CommonOption')->where('id', 250)->value('big_des');
            $auditApplyContent = model('CommonOption')->where('id', 260)->value('big_des');
            return view('', ['audit_apply_notice'=>$auditApplyNotice, 'audit_apply_content'=>$auditApplyContent]);
        }
    }
}